var ajax_datatable;
var TableDatatablesAjax = function () {
    var datatableAjax = function () {
        dt = $('.dataTablesAjax');
        ajax_datatable = dt.DataTable({
            "processing": true,
            "stateSave":true,
            "serverSide": true,
            "searching": true,
            "searchDelay": 800,
            "search": {
                "regex": true
            },
            "ajax": {
                'url': '/admin/apply/ajaxIndex'
            },
            "pagingType": "full_numbers",
            "deferRender": true,//延迟渲染
            "orderCellsTop": true,
            "dom": '<"html5buttons"B>lTfgitp',
            "buttons": [
                {extend: 'copy', title: 'permission'},
                {extend: 'csv', title: 'permission'},
                {extend: 'excel', title: 'permission'},
                {extend: 'pdf', title: 'permission'},
                {
                    extend: 'print',
                    customize: function (win) {
                        $(win.document.body).addClass('white-bg');
                        $(win.document.body).css('font-size', '10px');
                        $(win.document.body).find('table')
                            .addClass('compact')
                            .css('font-size', 'inherit');
                    }
                }
            ],
            "columns": [
                {
                    "data": "id",
                    "name": "id",
                    "orderable": true
                },
                {
                    "data": "apply",
                    "name": "apply",
                    "orderable": true
                },
                {
                    "data": "name",
                    "name": "name",
                    "orderable": true
                },
                {
                    "data": "province",
                    "name": "province",
                    "orderable": true
                },
                {
                    "data": "city",
                    "name": "city",
                    "orderable": true
                },
                {
                    "data": "school",
                    "name": "school",
                    "orderable": true
                },
                {
                    "data": "degree",
                    "name": "degree",
                    "orderable": true
                },
                {
                    "data": "grade",
                    "name": "grade",
                    "orderable": true
                },
                {
                    "data": "created_at",
                    "name": "created_at",
                    "orderable": true
                },
                {
                    "data": "stage",
                    "name": "stage",
                    "orderable": true
                },
                {
                    "data": "status",
                    "name": "status",
                    "orderable": true
                },
                {
                    "data": "actionButton",
                    "name": "actionButton",
                    "orderable": false
                }
            ],
            "drawCallback": function (settings) {
                ajax_datatable.$('.tooltips').tooltip({
                    placement: 'top',
                    html: true
                });

                var $applyTh = ajax_datatable.columns('.apply-th').nodes().flatten().to$();
                $applyTh.each(function (i, item) {
                    item = $(item);
                    item.html(transformApply(item.text()));
                });
                var $provinceTh = ajax_datatable.columns('.province-th').nodes().flatten().to$();
                $provinceTh.each(function (i, item) {
                    item = $(item);
                    item.html(transformProvince(item.text()));
                });
                var $degreeTh = ajax_datatable.columns('.degree-th').nodes().flatten().to$();
                $degreeTh.each(function (i, item) {
                    item = $(item);
                    item.html(transformDegree(item.text()));
                });
                var $gradeTh = ajax_datatable.columns('.grade-th').nodes().flatten().to$();
                $gradeTh.each(function (i, item) {
                    item = $(item);
                    item.html(transformGrade(item.text()));
                });
                var $stageTh = ajax_datatable.columns('.stage-th').nodes().flatten().to$();
                $stageTh.each(function (i, item) {
                    item = $(item);
                    item.html(transformStage(item.text()));
                });
                var $statusTh = ajax_datatable.columns('.status-th').nodes().flatten().to$();
                $statusTh.each(function (i, item) {
                    item = $(item);
                    item.html(transformStatus(item.text()));
                });

                //记忆选择框
                $(ajax_datatable.state().columns).each(function (i, item) {
                    var $search = item.search.search;
                    if($search!=='' && $search!=='false'){
                        $('#col' + i + '_filter').val($search);
                    }
                });
            },
            "language": {
                url: '/admin/i18n'
            }
        });
    };
    return {
        init: datatableAjax
    }
}();

$(function () {
    TableDatatablesAjax.init();

    $('select.column-filter').on('change', function () {
        var index = $(this).attr('data-table-index');
        ajax_datatable.column(index).search($('#col' + index + '_filter').val()).draw();
    });
});